package com.solidumgame.locroninja.juego
{
	
	import flash.geom.Vector3D;
	
	import libs.away3d.containers.ObjectContainer3D;
	import libs.away3d.core.base.Mesh;
	import libs.away3d.core.utils.Cast;
	import libs.away3d.loaders.Collada;
	import libs.away3d.loaders.Max3DS;
	import libs.away3d.materials.BitmapMaterial;
	import libs.away3d.materials.WireColorMaterial;
	import libs.away3d.primitives.Cube;
	import libs.jiglib.geometry.JBox;
	import libs.jiglib.plugin.away3d.Away3dMesh;
	
	
	public class Olla
	{
		protected var contenedorOlla:ObjectContainer3D;
		protected var piso:Cube;
		protected var sobrePiso:Cube;
		protected var paredIzquierda:Cube;
		protected var paredDerecha:Cube;
		protected var paredFondo:Cube;
		protected var paredFrente:Cube;
		

		public var pisoFisica:JBox;
		protected var paredIzquierdaFisica:JBox;
		protected var paredDerechaFisica:JBox;
		protected var paredFondoFisica:JBox;
		protected var paredFrenteFisica:JBox;
	
				
		[Embed(source="./assets/texturas/olla/horno.png")] // MIME-type important!
		private var HornoData : Class;
		
		[Embed(source="./assets/texturas/olla/mesa.png")] // MIME-type important!
		private var MesaData : Class;
		
		[Embed(source="./assets/texturas/olla/tren.jpg")] // MIME-type important!
		private var OllaData : Class;
		
		[Embed(source="./assets/texturas/olla/pared.jpg")] // MIME-type important!
		private var ParedData : Class;
		
		[Embed(source="./assets/texturas/olla/locroescena.3ds",mimeType="application/octet-stream")] // MIME-type important!
		private var LocroEscena : Class;
		
		
		
		public function Olla()
		{
			
			
			
			//Cargamos escena
			
			var olla3D:ObjectContainer3D;
			olla3D = Max3DS.parse(LocroEscena, {autoLoadTextures:false, scaling:170, y:-320, x:0, z:0, rotationX:90 });

			/*
			var olla3D:ObjectContainer3D;
			olla3D = Collada.parse(LocroEscena, {texturePath:"./assets/texturas/", autoLoadTextures:true, scaling:170, y:-320, x:0, z:0, rotationX:90});
			*/
			
			//Asignamos texturas
			
			(olla3D.children[0] as Mesh).material = new BitmapMaterial(Cast.bitmap(MesaData));
			(olla3D.children[1] as Mesh).material = new BitmapMaterial(Cast.bitmap(ParedData));
			(olla3D.children[2] as Mesh).material = new BitmapMaterial(Cast.bitmap(HornoData));
			(olla3D.children[3] as Mesh).material = new BitmapMaterial(Cast.bitmap(MesaData));
			(olla3D.children[4] as Mesh).material = new BitmapMaterial(Cast.bitmap(MesaData));
			(olla3D.children[5] as Mesh).material = new BitmapMaterial(Cast.bitmap(OllaData));
			
			/*
			(olla3D.children[0] as Mesh).visible = false;
			(olla3D.children[1] as Mesh).visible = false;
			(olla3D.children[2] as Mesh).visible = false;
			(olla3D.children[3] as Mesh).visible = false;
			(olla3D.children[4] as Mesh).visible = false;
			*/		
			
			Partida.escenario.scene.addChild(olla3D);
			
			//Agregamos olla de fisica
			contenedorOlla = new ObjectContainer3D();
			
			piso = new Cube({height:10,width:400,depth:400});
			paredIzquierda = new Cube({height:150,width:10,depth:400});
			paredDerecha = new Cube({height:150,width:10,depth:400});
			paredFondo = new Cube({height:150,width:400,depth:10});
			paredFrente = new Cube({height:150,width:400,depth:10});		
			
			contenedorOlla.addChild(piso);
			contenedorOlla.addChild(paredIzquierda);
			contenedorOlla.addChild(paredDerecha);
			contenedorOlla.addChild(paredFrente);
			contenedorOlla.addChild(paredFondo);
			
			
			pisoFisica = new JBox(new Away3dMesh(piso),400,400,10);
			
			pisoFisica.mass = 1000;
			pisoFisica.friction = 1;
			paredIzquierdaFisica = new JBox(new Away3dMesh(paredIzquierda),10,400,150);
			paredDerechaFisica = new JBox(new Away3dMesh(paredDerecha),10,600,150);
			paredFondoFisica = new JBox(new Away3dMesh(paredFrente),400,10,150);
			paredFrenteFisica = new JBox(new Away3dMesh(paredFondo),400,10,150);
			
			
			pisoFisica.movable = false;
			paredIzquierdaFisica.movable = false;
			paredDerechaFisica.movable = false;
			paredFondoFisica.movable = false;
			paredFrenteFisica.movable = false;
			
			
			pisoFisica.moveTo(new Vector3D(0,-235,0));
			paredIzquierdaFisica.moveTo(new Vector3D(-195,-235,0));
			paredDerechaFisica.moveTo(new Vector3D(195,-235,0));
			paredFondoFisica.moveTo(new Vector3D(0,-235,195));
			paredFrenteFisica.moveTo(new Vector3D(0,-235,-195));
			
		}
		public function agregarOlla(): void{
			//Partida.escenario.scene.addChild(contenedorOlla);
			
			Partida.fisica.addBody(pisoFisica);
			Partida.fisica.addBody(paredDerechaFisica);
			Partida.fisica.addBody(paredIzquierdaFisica);
			Partida.fisica.addBody(paredFrenteFisica);
			Partida.fisica.addBody(paredFondoFisica);
			
			
		}
	}
}